﻿using SDLib;
using SoundDesigner.Samples;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp1.bestFitting
{
    public class FittingHelper
    {

        #region Product Manager 
        public static ProductManager productManager { get; set; }
        public static string LibraryInfo = "";
        public static IDeviceInfo deviceInfo;
        private static IParameterMemory memory;
        private static IParameterList parameters;
        private static int WDRC_NumberOfChannels;
        private static int EQ_NumberOfChannels;
        #endregion
        #region GlobalData
        /// <summary>
        /// 
        /// </summary>
        public static int[] TargetInput = new int[] { 40, 60, 80 };
        /// <summary>
        /// 听力图曲线 频点
        /// </summary>
        public static double[] TargetFreqList = new double[] { 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000 };
        /// <summary>
        /// 听力图曲线 听力值
        /// </summary>
        public static int[] ACAugiogram = new int[] { 60, 60, 60, 60, 60, 60, 60, 60, 60 };

        /// <summary>
        /// 目标曲线
        /// </summary>
        public static double[] TargetLowGain = new double[] { };
        public static double[] TargetMidGain = new double[] { };
        public static double[] TargetHighGain = new double[] { };
        //public static double[] TargetGain50 = new double[] { };

        public static double[] TargetLowOut = new double[] { };
        public static double[] TargetMidOut = new double[] { };
        public static double[] TargetHighOut = new double[] { };
        //public static double[] TargetOut50 = new double[] { };


        public static double[] TargetCurveLow = new double[] { };
        public static double[] TargetCurveMid = new double[] { };
        public static double[] TargetCurveHigh = new double[] { };
        //public static double[] TargetCurve50 = new double[] { };

        /// <summary>
        /// 频响曲线数据
        /// </summary>
        //默认频响曲线频率列表
        public static double[] FrequencyList = new double[] { };
        public static double[] ArrDataList40 = new double[] { };
        public static double[] ArrDataList60 = new double[] { };
        public static double[] ArrDataList80 = new double[] { };

        public static double[] FrequencyOutList = new double[] { };
        public static double[] ArrDataCurveList40 = new double[] { };
        public static double[] ArrDataCurveList60 = new double[] { };
        public static double[] ArrDataCurveList80 = new double[] { };


        /// <summary>
        /// IO曲线
        /// </summary>
        public static double[] InputList = new double[] { };
        public static double[] ArrDataList200 = new double[] { };
        public static double[] ArrDataList1000 = new double[] { };
        public static double[] ArrDataList2000 = new double[] { };
        public static double[] ArrDataList4000 = new double[] { };

        
    #endregion

    #region FittingData
    public static double[] WDRC_CrossoverFrequency = new double[] { };
        public static double[] LTHs = new double[] { };
        public static double[] LLGs = new double[] { };
        public static double[] LLGs_MIN = new double[] { };
        public static double[] LLGs_MAX = new double[] { };
        public static double[] HTHs = new double[] { };
        public static double[] HLGs = new double[] { };
        public static double[] HLGs_MIN = new double[] { };
        public static double[] HLGs_MAX = new double[] { };
        public static string[] ERs = new string[] { };
        public static double[] EQ_CrossoverFrequency = new double[] { };
        public static double[] EQs = new double[] { };
        public static double[] EQs_MIN = new double[] { };
        public static double[] EQs_MAX = new double[] { };


        #endregion

        public static void InitProductManager()
        {
            productManager = new ProductManager();
            LibraryInfo = $"{Constants.ConnectedDevice}.library";
            productManager.Initialize(LibraryInfo, 0, productName: Constants.ProductName, programmer: Constants.Programmer);
            Console.WriteLine($"ProductManager is initialized");
        }
        public static void Clear(int index = 0) {

            FittingHelper.memory = productManager.GetMemory(index);
            FittingHelper.parameters = memory.Parameters;
            var param_ChannelCount = parameters.GetById($"X_WDRC_NumberOfChannels");
            var WDRC_NumberOfChannels = (int)GetValueFromListValues(param_ChannelCount);
            var param_EQCount = parameters.GetById($"X_EQ_NumberOfChannels");
            var EQ_NumberOfChannels = (int)GetValueFromListValues(param_EQCount);
            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                SetValue(parameters.GetById($"X_WDRC_LowLevelThreshold[{i}]"), 40);
                SetValue(parameters.GetById($"X_WDRC_HighLevelThreshold[{i}]"), 80);
                SetValue(parameters.GetById($"X_WDRC_ChannelOutputLimit[{i}]"), 0);
                SetValue(parameters.GetById($"X_WDRC_LowLevelGain[{i}]"), 0);
                SetValue(parameters.GetById($"X_WDRC_HighLevelGain[{i}]"), 0);
            }
            for (int i = 0; i < EQ_NumberOfChannels; i++)
            {
                SetValue(parameters.GetById($"X_EQ_ChannelGain_dB[{i}]"), 0);
            }

        }
        public  static void InitFitting(int index=0)
        {
            FittingHelper.memory = productManager.GetMemory(index);
            FittingHelper.parameters = memory.Parameters;
            var param_ChannelCount = parameters.GetById($"X_WDRC_NumberOfChannels");
            WDRC_NumberOfChannels = (int)GetValueFromListValues(param_ChannelCount);
            var param_EQCount = parameters.GetById($"X_EQ_NumberOfChannels");
            EQ_NumberOfChannels = (int)GetValueFromListValues(param_EQCount);

            WDRC_CrossoverFrequency = new double[(int)(WDRC_NumberOfChannels - 1)];
            LTHs = new double[(int)(WDRC_NumberOfChannels)];
            HTHs = new double[(int)(WDRC_NumberOfChannels)];
            LLGs = new double[(int)(WDRC_NumberOfChannels)];
            HLGs = new double[(int)(WDRC_NumberOfChannels)];
            ERs = new string[(int)(WDRC_NumberOfChannels)];

            EQ_CrossoverFrequency = new double[(int)(EQ_NumberOfChannels)];
            EQs = new double[(int)(EQ_NumberOfChannels)];
            SetValue(parameters.GetById($"X_WDRC_Enable"), "Enabled");
            SetValue(parameters.GetById($"X_EQ_Enable"), "Enabled");
            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                SetValue(parameters.GetById($"X_WDRC_LowLevelThreshold[{i}]"), 50);
                SetValue(parameters.GetById($"X_WDRC_HighLevelThreshold[{i}]"), 80);
                SetValue(parameters.GetById($"X_WDRC_ChannelOutputLimit[{i}]"), 0);
                if (i <(WDRC_NumberOfChannels-1))
                {
                    WDRC_CrossoverFrequency[i]= (double)GetValue(parameters.GetById($"X_WDRC_CrossoverFrequency[{i}]"));
                }
                LTHs[i] = (double)GetValue(parameters.GetById($"X_WDRC_LowLevelThreshold[{i}]"));
                HTHs[i] = (double)GetValue(parameters.GetById($"X_WDRC_HighLevelThreshold[{i}]"));

                LLGs[i] = (double)GetValue(parameters.GetById($"X_WDRC_LowLevelGain[{i}]"));
                HLGs[i] = (double)GetValue(parameters.GetById($"X_WDRC_HighLevelGain[{i}]"));
                ERs[i] = (string)GetValue(parameters.GetById($"X_WDRC_ExpansionRatio[{i}]"));
            }
            for (int i = 0; i < EQ_NumberOfChannels; i++)
            {
                EQ_CrossoverFrequency[i] = (double)GetValue(parameters.GetById($"X_EQ_CrossoverFrequency[{i}]"));
                EQs[i] = (double)GetValue(parameters.GetById($"X_EQ_ChannelGain_dB[{i}]"));
            }

            //按照频响曲线频点进行插值，便于通道增益计算
            FittingHelper.FrequencyOutList = getFequencyOutList();
            //TargetCurve50 = Module_Calculate.Interpolation(TargetFreqList, TargetOut50, FrequencyOutList);
            TargetCurveLow = Module_Calculate.Interpolation(TargetFreqList, TargetLowOut, FrequencyOutList);
            TargetCurveMid = Module_Calculate.Interpolation(TargetFreqList, TargetMidOut, FrequencyOutList);
            TargetCurveHigh = Module_Calculate.Interpolation(TargetFreqList, TargetHighOut, FrequencyOutList);
        }

        public static bool Autofit(int index = 0)
        {
            //InitFitting(index);
            //ShowFittingParam();
            Calculate();
            //ShowFittingParam();
            return true;
        }

        /// <summary>
        /// 目标曲线是 FIG6 时
        /// </summary>
        public static void Calculate_WDRC() {
            GetCurveOutData();
            var DiffL = CalculateDiff(TargetCurveLow, ArrDataCurveList40);
            var DiffM = CalculateDiff(TargetCurveMid, ArrDataCurveList60);
            var DiffH = CalculateDiff(TargetCurveHigh, ArrDataCurveList80);
            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                var llg_temp = new List<double>();
                var mlg_temp = new List<double>();
                var hlg_temp = new List<double>();
                var start = 0d;
                var end = 12000d;
                if (i == 0)
                {
                    start = 10;
                    end = WDRC_CrossoverFrequency[i];
                }
                else if (i == WDRC_NumberOfChannels - 1)
                {
                    start = WDRC_CrossoverFrequency[i - 1];
                    end = 12000;
                }
                else
                {
                    start = WDRC_CrossoverFrequency[i - 1];
                    end = WDRC_CrossoverFrequency[i];
                }
                if (i == WDRC_NumberOfChannels - 1)
                {
                    for (int j = 0; j < DiffL.Length; j++)
                    {
                        if (FrequencyOutList[j] >= start && FrequencyOutList[j] <= end)
                        {
                            if (FrequencyOutList[j] < start || FrequencyOutList[j] > end)
                            {
                                llg_temp.Add(DiffL[j] * 0.25);
                                mlg_temp.Add(DiffM[j] * 0.25);
                                hlg_temp.Add(DiffH[j] * 0.25);
                            }
                            else
                            {
                                llg_temp.Add(DiffL[j]);
                                mlg_temp.Add(DiffM[j]);
                                hlg_temp.Add(DiffH[j]);
                            }
                        }
                        // 超出当前通道范围，停止计算
                        if (FrequencyOutList[j] > end)
                        {
                            break;
                        }
                    }

                }
                else if (i == 0)
                {
                    for (int j = 0; j < DiffL.Length; j++)
                    {
                        if (FrequencyOutList[j] >= 0 && FrequencyOutList[j] <= end)
                        {
                            if (FrequencyOutList[j] < start || FrequencyOutList[j] > end)
                            {
                                llg_temp.Add(DiffL[j] * 0.25);
                                mlg_temp.Add(DiffM[j] * 0.25);
                                hlg_temp.Add(DiffH[j] * 0.25);
                            }
                            else
                            {
                                llg_temp.Add(DiffL[j]);
                                mlg_temp.Add(DiffM[j]);
                                hlg_temp.Add(DiffH[j]);
                            }
                        }
                        // 超出当前通道范围，停止计算
                        if (FrequencyOutList[j] > end)
                        {
                            break;
                        }
                    }

                }
                else
                {
                    for (int j = 0; j < DiffL.Length; j++)
                    {
                        if (FrequencyOutList[j] >= start && FrequencyOutList[j] <= end)
                        {

                            if (FrequencyOutList[j] < start || FrequencyOutList[j] > end)
                            {
                                llg_temp.Add(DiffL[j] * 0.25);
                                mlg_temp.Add(DiffM[j] * 0.25);
                                hlg_temp.Add(DiffH[j] * 0.25);
                            }
                            else
                            {
                                llg_temp.Add(DiffL[j]);
                                mlg_temp.Add(DiffM[j]);
                                hlg_temp.Add(DiffH[j]);
                            }

                        }
                        // 超出当前通道范围，停止计算
                        if (FrequencyOutList[j] > end)
                        {
                            break;
                        }
                    }

                }
                var VariationL = Module_Calculate.Mean_weight(llg_temp);
                var VariationM = Module_Calculate.Mean_weight(mlg_temp);
                var VariationH = Module_Calculate.Mean_weight(hlg_temp);

                VariationH = Math.Min(VariationL, VariationH);
                LLGs[i] += VariationL;
                HLGs[i] += VariationH;
                Debug.WriteLine($"{i} {start} {end} {llg_temp.Average()} {VariationL} {hlg_temp.Average()} {VariationH}");

                SetValue(parameters.GetById($"X_WDRC_LowLevelGain[{i}]"), LLGs[i]);
                SetValue(parameters.GetById($"X_WDRC_HighLevelGain[{i}]"), HLGs[i]);
            }
        }

        public static void Calculate_EQ()
        {
            GetCurveOutData();
            var DiffL = CalculateDiff(TargetCurveLow, ArrDataCurveList40);
            var DiffM = CalculateDiff(TargetCurveMid, ArrDataCurveList60);
            var DiffH = CalculateDiff(TargetCurveHigh, ArrDataCurveList80);
            var Diff = new double[DiffL.Length];
            for (int i = 0; i < Diff.Length; i++)
            {
                Diff[i] = 0.5 * DiffL[i] + 0.35 * DiffM[i] + 0.15 * DiffH[i];
            }

            for (int i = 0; i < EQ_NumberOfChannels; i++)
            {
                var llg_temp = new List<double>();
                var start = 0d;
                var end = 12000d;
                if (i == 0)
                {
                    start = 10;
                    end = EQ_CrossoverFrequency[i];
                }
                else if (i == EQ_NumberOfChannels - 1)
                {
                    start = EQ_CrossoverFrequency[i - 1];
                    end = 12000;
                }
                else
                {
                    start = EQ_CrossoverFrequency[i - 1];
                    end = EQ_CrossoverFrequency[i];
                }
                if (i == EQ_NumberOfChannels - 1)
                {
                    for (int j = 0; j < Diff.Length; j++)
                    {
                        if (FrequencyOutList[j] >= start && FrequencyOutList[j] <= end)
                        {
                            if (FrequencyOutList[j] < start || FrequencyOutList[j] > end)
                            {
                                llg_temp.Add(Diff[j] * 0.25);
                            }
                            else
                            {
                                llg_temp.Add(Diff[j]);
                            }
                        }
                        // 超出当前通道范围，停止计算
                        if (FrequencyOutList[j] > end)
                        {
                            break;
                        }
                    }

                }
                else if (i == 0)
                {
                    for (int j = 0; j < Diff.Length; j++)
                    {
                        if (FrequencyOutList[j] >= 0 && FrequencyOutList[j] <= end)
                        {
                            if (FrequencyOutList[j] < start || FrequencyOutList[j] > end)
                            {
                                llg_temp.Add(Diff[j] * 0.25);
                            }
                            else
                            {
                                llg_temp.Add(Diff[j]);
                            }
                        }
                        // 超出当前通道范围，停止计算
                        if (FrequencyOutList[j] > end)
                        {
                            break;
                        }
                    }

                }
                else
                {
                    for (int j = 0; j < Diff.Length; j++)
                    {
                        if (FrequencyOutList[j] >= start && FrequencyOutList[j] <= end)
                        {

                            if (FrequencyOutList[j] < start || FrequencyOutList[j] > end)
                            {
                                llg_temp.Add(Diff[j] * 0.25);
                            }
                            else
                            {
                                llg_temp.Add(Diff[j]);
                            }

                        }
                        // 超出当前通道范围，停止计算
                        if (FrequencyOutList[j] > end)
                        {
                            break;
                        }
                    }

                }
                var VariationEQ = Module_Calculate.Mean_weight(llg_temp);
                var  EQTemp= EQs[i] - VariationEQ;
                if (EQTemp>18)
                {
                    EQs[i] = 18;
                }else if (EQTemp < -18)
                {
                    EQs[i] = -18;
                }
                else
                {
                    EQs[i] = EQTemp;
                }

                Debug.WriteLine($"{i} {start} {end} {llg_temp.Average()} {VariationEQ} {llg_temp.Average()}");

                SetValue(parameters.GetById($"X_EQ_ChannelGain_dB[{i}]"), EQs[i]);
            }
        }

        public static void Calculate()
        {
            Calculate_WDRC();
            Calculate_EQ();
        }
        private static double[] CalculateDiff(double[] Target, double[] Current)
        {
            var Diff = new double[Target.Length];
            for (int i = 0; i < Target.Length; i++)
            {
                Diff[i] = Target[i] - Current[i];
            }
            return Diff;
        }

        private static void ShowFittingParam() {
            var param_ChannelCount = parameters.GetById($"X_WDRC_NumberOfChannels");
            var WDRC_NumberOfChannels = GetValueFromListValues(param_ChannelCount);
            var param_EQCount = parameters.GetById($"X_EQ_NumberOfChannels");
            var EQ_NumberOfChannels = GetValueFromListValues(param_EQCount);
            List<IParameter> parameters1 = new List<IParameter>();
            //parameters1.Add(parameters.GetById($"X_WDRC_Enable"));
            //parameters1.Add(parameters.GetById($"X_WDRC_NumberOfChannels"));
            //parameters1.Add(parameters.GetById($"X_EQ_Enable"));
            //parameters1.Add(parameters.GetById($"X_EQ_NumberOfChannels"));

            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                parameters1.Add(parameters.GetById($"X_WDRC_LowLevelThreshold[{i}]"));
            }
            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                parameters1.Add(parameters.GetById($"X_WDRC_HighLevelThreshold[{i}]"));
            }
            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                parameters1.Add(parameters.GetById($"X_WDRC_LowLevelGain[{i}]"));
            }
            for (int i = 0; i < WDRC_NumberOfChannels; i++)
            {
                parameters1.Add(parameters.GetById($"X_WDRC_HighLevelGain[{i}]"));
            }
            //for (int i = 0; i < WDRC_NumberOfChannels; i++)
            //{
            //    //if (i < (WDRC_NumberOfChannels - 1))
            //    //{
            //    //    parameters1.Add(parameters.GetById($"X_WDRC_CrossoverFrequency[{i}]"));
            //    //}
            //    parameters1.Add(parameters.GetById($"X_WDRC_LowLevelThreshold[{i}]"));
            //    parameters1.Add(parameters.GetById($"X_WDRC_LowLevelGain[{i}]"));
            //    parameters1.Add(parameters.GetById($"X_WDRC_HighLevelThreshold[{i}]"));
            //    parameters1.Add(parameters.GetById($"X_WDRC_HighLevelGain[{i}]"));
            //    //parameters1.Add(parameters.GetById($"X_WDRC_ChannelOutputLimit[{i}]"));
            //    //parameters1.Add(parameters.GetById($"X_WDRC_ExpansionRatio[{i}]"));
            //}
            //for (int i = 0; i < EQ_NumberOfChannels; i++)
            //{
            //    //parameters1.Add(parameters.GetById($"X_EQ_CrossoverFrequency[{i}]"));
            //    parameters1.Add(parameters.GetById($"X_EQ_ChannelGain_dB[{i}]"));
            //}
            parameters1.OrderBy(it => it.Id);
            foreach (var param in parameters1)
            {
                //var logData = $"Type:{param.Type};Id:{param.Id};Name:{param.Name};Value:{param.Value}";
                var logData = $"{param.Id} = ";
                switch (param.Type)
                {
                    case ParameterType.kUnknownType:
                    case ParameterType.kInteger:
                    case ParameterType.kDouble:
                    case ParameterType.kBoolean:
                        logData = $"{logData} {param.Value}";
                        break;
                    case ParameterType.kIndexedList:
                        var ValueData = (double)GetValue(param);
                        //logData = $"{logData};ValueData:{ValueData} {param.Units}";
                        logData = $"{logData} {ValueData} ";
                        break;
                    case ParameterType.kIndexedTextList:
                        var stringData = (string)GetValue(param);
                        //logData = $"{logData};StringData:{stringData} {param.Units}";
                        logData = $"{logData} {stringData} ";
                        break;
                    case ParameterType.kByte:
                        break;
                    default:
                        break;
                }
                Debug.WriteLine($"{logData}");

            }

        }

        public static double SetValue(IParameter parameter, object Value) {
            var ValueIndex = 0d;
            if (parameter.Type == ParameterType.kIndexedList)
            {
                ValueIndex= SetValue(parameter, (double)Value);
            }

            if (parameter.Type == ParameterType.kIndexedTextList)
            {
                ValueIndex= SetValue(parameter, (string)Value);
            }
            return ValueIndex;
        }
        public static double SetValue(IParameter parameter, double Value)
        {
            var ListValues = parameter.ListValues;
            int Offset = 0;
            foreach (var item in ListValues)
            {
                 
                if (Value == (double)item) {
                    parameter.Value = Offset;
                    break;
                }
                Offset++;
            }
            return Offset;
        }
        public static double SetValue(IParameter parameter, string Value)
        {
            var ListValues = parameter.TextListValues;
            int Offset = 0;
            foreach (var item in ListValues)
            {
                if (Value.Equals((string)item))
                {
                    parameter.Value = Offset;
                    break;
                }
                Offset++;
            }
            return Offset;
        }

        public static object GetValue(IParameter parameter)
        {
            if (parameter.Type == ParameterType.kIndexedList)
            {
                return GetValueFromListValues(parameter);
            }

            if (parameter.Type == ParameterType.kIndexedTextList)
            {
                return GetValueFromTextListValues(parameter);
            }
            return null;
        }

        public static double GetValueFromListValues(IParameter parameter)
        {
            var ListValues = parameter.ListValues;
            int index = parameter.Value;
            int Offset = 0;
            foreach (var item in ListValues)
            {
                if (Offset == index) return (double)item;
                Offset++;
            }
            return 0;
        }
        public static string GetValueFromTextListValues(IParameter parameter)
        {
            var ListValues = parameter.TextListValues;
            int index = parameter.Value;
            int Offset = 0;
            foreach (var item in ListValues)
            {
                if (Offset == index) return (string)item;
                Offset++;
            }
            return "";
        }

        public static void GetTargetCurveData()
        {
            double[] ArrLowGainData = new double[] { };
            double[] ArrMidGainData = new double[] { };
            double[] ArrHightGainData = new double[] { };
            FFig6.GetTargetCurveData(FittingHelper.ACAugiogram, ref ArrLowGainData, ref ArrMidGainData, ref ArrHightGainData);
            FittingHelper.TargetLowGain = ArrLowGainData;
            FittingHelper.TargetMidGain = ArrMidGainData;
            FittingHelper.TargetHighGain = ArrHightGainData;
            //FittingHelper.TargetGain50 = new double[FittingHelper.TargetFreqList.Length];
            FittingHelper.TargetLowOut = new double[FittingHelper.TargetFreqList.Length];
            FittingHelper.TargetMidOut = new double[FittingHelper.TargetFreqList.Length];
            FittingHelper.TargetHighOut = new double[FittingHelper.TargetFreqList.Length];
            for (int i = 0; i < FittingHelper.TargetFreqList.Length; i++)
            {
                FittingHelper.TargetLowOut[i] = ArrLowGainData[i] + FittingHelper.TargetInput[0];
                FittingHelper.TargetMidOut[i] = ArrMidGainData[i] + FittingHelper.TargetInput[1];
                FittingHelper.TargetHighOut[i] = ArrHightGainData[i] + FittingHelper.TargetInput[2];
            }
            //Module_Calculate.InterpolationTarget50(40, FittingHelper.TargetLowOut, 60, FittingHelper.TargetMidOut, 50, out double[] Target50);
            //FittingHelper.TargetOut50 = Target50;
            //for (int i = 0; i < FittingHelper.TargetLowOut.Length; i++)
            //{
            //    FittingHelper.TargetGain50[i] = FittingHelper.TargetOut50[i] - 50;
            //    Debug.WriteLine($"{i} {FittingHelper.TargetLowOut[i]} {FittingHelper.TargetMidOut[i]} {Target50[i]} ");
            //}

        }

        public static void GetCurveData()
        {
            try
            {
                FittingHelper.FrequencyList = getFequencyList();
                FittingHelper.ArrDataList40 = productManager.GetGraphData(GraphId.kFrequencyResponseGainGraph, FittingHelper.FrequencyList, FittingHelper.FrequencyList.Length, FittingHelper.TargetInput[0]);
                //FittingHelper.ArrDataList50 = productManager.GetGraphData(GraphId.kFrequencyResponseGainGraph, FittingHelper.FrequencyList, FittingHelper.FrequencyList.Length, FittingHelper.TargetInput[1]);
                FittingHelper.ArrDataList60 = productManager.GetGraphData(GraphId.kFrequencyResponseGainGraph, FittingHelper.FrequencyList, FittingHelper.FrequencyList.Length, FittingHelper.TargetInput[1]);
                FittingHelper.ArrDataList80 = productManager.GetGraphData(GraphId.kFrequencyResponseGainGraph, FittingHelper.FrequencyList, FittingHelper.FrequencyList.Length, FittingHelper.TargetInput[2]);

                FittingHelper.InputList = getInputList();
                FittingHelper.ArrDataList200 = productManager.GetGraphData(GraphId.kInputOutputCurveGraph, FittingHelper.InputList, FittingHelper.InputList.Length, 200);
                FittingHelper.ArrDataList1000 = productManager.GetGraphData(GraphId.kInputOutputCurveGraph, FittingHelper.InputList, FittingHelper.InputList.Length, 1000);
                FittingHelper.ArrDataList2000 = productManager.GetGraphData(GraphId.kInputOutputCurveGraph, FittingHelper.InputList, FittingHelper.InputList.Length, 2000);
                FittingHelper.ArrDataList4000 = productManager.GetGraphData(GraphId.kInputOutputCurveGraph, FittingHelper.InputList, FittingHelper.InputList.Length, 4000);

            }
            catch (Exception ee)
            {
                MessageBox.Show($" {ee.Message}");
                //throw;
            }
            
        }
        public static void GetCurveOutData()
        {
            FittingHelper.FrequencyOutList = getFequencyOutList();
            FittingHelper.ArrDataCurveList40 = productManager.GetGraphData(GraphId.kFrequencyResponseGraph, FittingHelper.FrequencyOutList, FittingHelper.FrequencyOutList.Length, FittingHelper.TargetInput[0]);
            //FittingHelper.ArrDataList50 = productManager.GetGraphData(GraphId.kFrequencyResponseGraph, FittingHelper.FrequencyOutList, FittingHelper.FrequencyOutList.Length, FittingHelper.TargetInput[1]);
            FittingHelper.ArrDataCurveList60 = productManager.GetGraphData(GraphId.kFrequencyResponseGraph, FittingHelper.FrequencyOutList, FittingHelper.FrequencyOutList.Length, FittingHelper.TargetInput[1]);
            FittingHelper.ArrDataCurveList80 = productManager.GetGraphData(GraphId.kFrequencyResponseGraph, FittingHelper.FrequencyOutList, FittingHelper.FrequencyOutList.Length, FittingHelper.TargetInput[2]);

            Debug.WriteLine($"*******************************************");
            for (int i = 0; i < TargetCurveLow.Length; i++)
            {
                Debug.WriteLine($"{i} {FrequencyOutList[i]} {TargetCurveLow[i]} {TargetCurveHigh[i]} {ArrDataCurveList40[i]} {ArrDataCurveList80[i]}");
            }
            Debug.WriteLine($"*******************************************");
        }

        public static double[] getInputList()
        {
            int i = 0;
            double[] SixthOctaves = new double[101];

            for (i = 0; i < 101; i++)
            {
                SixthOctaves[i] = 20 + i;
            }
            return SixthOctaves;
        }

        public static double[] getFequencyOutList()
        {
            double[] SixthOctaves = new double[] {
                125,130,133,137,140,145,150,155,160,165,170,175,180,185,190,195,200,206,212,218,224,230,237,244,250,257,265,
                272,280,290,300,307,315,325,335,345,355,365,375,387,400,412,425,437,450,462,475,487,500,515,530,545,560,580,
                600,615,630,650,670,690,710,730,750,775,800,825,850,875,900,925,950,975,1000,1030,1060,1090,1120,1150,1180,
                1210,1250,1300,1330,1370,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2060,2120,2180,
                2240,2300,2370,2440,2500,2570,2650,2720,2800,2900,3000,3070,3150,3250,3350,3450,3550,3650,3750,3870,4000,
                4120,4250,4370,4500,4620,4750,4870,5000,5150,5300,5450,5600,5800,6000,6150,6300,6500,6700,6900,7100,7300,
                7500,7750,8000,8980,10079,11314 };

            //  double[] SixthOctaves = new double[]{125,140,157,177,198,223,250,281,315,354,397,445,500,561,630,707,794,891,1000,1122,
            //        1260,1414,1587,1782,2000,2245,2520,2828,3175,3564,4000,4490,5040,5657,6350,7127,8000,8980,10079,11314 };
            return SixthOctaves;
        }

        public static double[] getFequencyList()
        {
            // double[] SixthOctaves = new double[] {125,130,133,137,140,145,150,155,160,165,170,175,180,185,190,195,200,206,
            //   212,218,224,230,237,244,250,257,265,
            //    272,280,290,300,307,315,325,335,345,355,365,375,387,400,412,425,437,450,462,475,487,500,515,530,545,560,580,
            //    600,615,630,650,670,690,710,730,750,775,800,825,850,875,900,925,950,975,1000,1030,1060,1090,1120,1150,1180,
            //    1210,1250,1300,1330,1370,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2060,2120,2180,
            //    2240,2300,2370,2440,2500,2570,2650,2720,2800,2900,3000,3070,3150,3250,3350,3450,3550,3650,3750,3870,4000,
            //    4120,4250,4370,4500,4620,4750,4870,5000,5150,5300,5450,5600,5800,6000,6150,6300,6500,6700,6900,7100,7300,
            //    7500,7750,8000};
            //  double[] SixthOctaves = new double[]{125,140,157,177,198,223,250,281,315,354,397,445,500,561,630,707,794,891,1000,1122,
            //        1260,1414,1587,1782,2000,2245,2520,2828,3175,3564,4000,4490,5040,5657,6350,7127,8000,8980,10079,11314 };
            double[] SixthOctaves = new double[]{
                125     ,
                140     ,
                157     ,
                177     ,
                198     ,
                223     ,
                250     ,
                281     ,
                315     ,
                354     ,
                397     ,
                445     ,
                500     ,
                561     ,
                630     ,
                707     ,
                794     ,
                891     ,
                1000    ,
                1122    ,
                1260    ,
                1414    ,
                1587    ,
                1782    ,
                2000    ,
                2245    ,
                2520    ,
                2828    ,
                3175    ,
                3564    ,
                4000    ,
                4490    ,
                5040    ,
                5657    ,
                6350    ,
                7127    ,
                8000    ,
                8980    ,
                10079   ,
                11314 };

            return SixthOctaves;
        }
    }
}
